package com.asialjim.wechat.spring.controller;

import com.asialjim.wechat.database.CRUD_DataBaseService;
import com.asialjim.wechat.database.DataBaseService;
import com.asialjim.wechat.database.DataBaseTableInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <h1><em>ASIAL JIM JAVA DOC</em></h1><hr/>
 * <h2>CLASS DESCRIPTION <i>[ NAME: CRUD_DataBaseController ]</i></h2><strong>
 * <p> crud data base controller
 * </strong><p><p><em>Copyright &copy; Asial Jim Co., LTD</em><hr/>
 *
 * @author Asial Jim &nbsp;&nbsp; <span>Email:<a href="mailto:asialjim@hotmail.com">asialjim@hotmail.com</a> &nbsp;&nbsp; <a href="asialjim@qq.com">asialjim@qq.com</a></span>
 * @version 1.0.0
 * @since 2021/3/18   &nbsp;&nbsp; JDK 8
 */
@RestController
@RequestMapping("/table")
@Api("TABLE_SERVICE")
public class CRUD_DataBaseController {
    @Autowired
    private DataBaseService service;
    @Autowired
    private CRUD_DataBaseService dataBaseService;

    @PostMapping("/{tableName}")
    @ApiOperation("add record to data base")
    public Integer saveRecord(@PathVariable("tableName") String tableName, @RequestBody List<DataBaseTableInfo> tableInfos){
        return dataBaseService.add2Table(tableName, tableInfos);
    }

    @DeleteMapping("/{tableName}")
    @ApiOperation("delete record from data base")
    public Integer deleteRecord(@PathVariable("tableName")String tableName,@RequestBody List<DataBaseTableInfo> conditions){
        return dataBaseService.deleteRecord(tableName,conditions);
    }

    @PutMapping("/{tableName}")
    @ApiOperation("update record for data base")
    public Integer modifyRecord(@PathVariable("tableName")String tableName,@RequestBody CRUD_DataBaseController.CRUD_MODIFY_MODEL modify_model){
        return dataBaseService.updateRecord(tableName, modify_model.getCondition(), modify_model.getTarget());
    }

    @PostMapping("/{tableName}/{pageSize}/{pageNum}")
    @ApiOperation("query record from data base in page")
    public IPage<Object> page(@PathVariable("tableName") String tableName,@PathVariable("pageSize")Integer pageSize,@PathVariable("pageNum")Integer pageNum,@RequestBody List<DataBaseTableInfo> conditions){
        return dataBaseService.selectPage(tableName, pageSize, pageNum, conditions);
    }

    @GetMapping("/{tableName}")
    @ApiOperation("query all record in table from data base")
    public List<Object> all(@PathVariable("tableName")String tableName) throws Exception {
        return dataBaseService.all(tableName);
    }

    @GetMapping
    @ApiOperation("all tables")
    public Map<String,List<DataBaseTableInfo>> allTables(){
        return service.allTables();
    }


    @Data
    public static class CRUD_MODIFY_MODEL implements Serializable {
        private static final long serialVersionUID = 4933665201762822079L;

        private List<DataBaseTableInfo> condition;
        private List<DataBaseTableInfo> target;
    }
}
